package com.netrust.cmsx.entity.module.sys;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.validator.constraints.Length;

import com.google.common.collect.Lists;
import com.netrust.cmsx.core.persist.DataEntity;
import com.netrust.cmsx.utils.string.StringUtils;

/**
 * 
 * @author lazite
 *
 */
@Entity
@Table(name = "sys_role")
public class Role extends DataEntity<Role> {

	private static final long serialVersionUID = 1L;
	
	@Column(name = "name")
	private String name; 	// 角色名称
	@Column(name = "enname")
	private String enname;	// 英文名称
	@Column(name = "role_type")
	private String roleType;// 权限类型
	@Column(name = "data_scope")
	private String dataScope;// 数据范围
	@Column(name = "is_sys")
	private boolean sysData; 		//是否是系统数据
	@Column(name = "useable")
	private boolean useable; 		//是否是可用
	@Column(name = "del_flag")
	private boolean delFlag; 		//是否删除
	@Transient
	private boolean checked =false; 		//是否选中【该属性适用于use的jsp页面check选择】

	@Transient
	private String oldName; 	// 原角色名称
	@Transient
	private String oldEnname;	// 原英文名称
	
	@ManyToMany(mappedBy = "roles", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	private List<User> users = Lists.newArrayList();	
	
	@ManyToMany(mappedBy = "roles",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	private List<Menu> menus = Lists.newArrayList(); // 拥有菜单列表

	public Role() {
		super();
		this.useable=true;
	}
	
	public Role(String id){
		super(id);
	}
	

	public boolean getUseable() {
		return useable;
	}

	public void setUseable(boolean useable) {
		this.useable = useable;
	}

	public boolean getSysData() {
		return sysData;
	}

	public void setSysData(boolean sysData) {
		this.sysData = sysData;
	}
	
	public boolean getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(boolean delFlag) {
		this.delFlag = delFlag;
	}

	@Length(min=1, max=100)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Length(min=1, max=100)
	public String getEnname() {
		return enname;
	}

	public void setEnname(String enname) {
		this.enname = enname;
	}
	
	@Length(min=1, max=100)
	public String getRoleType() {
		return roleType;
	}

	public void setRoleType(String roleType) {
		this.roleType = roleType;
	}

	public String getDataScope() {
		return dataScope;
	}

	public void setDataScope(String dataScope) {
		this.dataScope = dataScope;
	}

	
	public boolean getChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
	}

	public String getOldName() {
		return oldName;
	}

	public void setOldName(String oldName) {
		this.oldName = oldName;
	}

	public String getOldEnname() {
		return oldEnname;
	}

	public void setOldEnname(String oldEnname) {
		this.oldEnname = oldEnname;
	}

//	public List<User> getUserList() {
//		return userList;
//	}
//
//	public void setUserList(List<User> userList) {
//		this.userList = userList;
//	}
//	
//	public List<String> getUserIdList() {
//		List<String> nameIdList = Lists.newArrayList();
//		for (User user : userList) {
//			nameIdList.add(user.getId());
//		}
//		return nameIdList;
//	}
//
//	public String getUserIds() {
//		return StringUtils.join(getUserIdList(), ",");
//	}

	public List<Menu> getMenus() {
		return menus;
	}

	public void setMenus(List<Menu> menus) {
		this.menus = menus;
	}

	public List<String> getMenuIdList() {
		List<String> menuIdList = Lists.newArrayList();
		for (Menu menu : menus) {
			menuIdList.add(menu.getId());
		}
		return menuIdList;
	}

	public void setMenuIdList(List<String> menuIdList) {
		menus = Lists.newArrayList();
		for (String menuId : menuIdList) {
			Menu menu = new Menu();
			menu.setId(menuId);
			menus.add(menu);
		}
	}

	public String getMenuIds() {
		return StringUtils.join(getMenuIdList(), ",");
	}
	
	public void setMenuIds(String menuIds) {
		menus = Lists.newArrayList();
		if (menuIds != null){
			String[] ids = StringUtils.split(menuIds, ",");
			setMenuIdList(Lists.newArrayList(ids));
		}
	}

	/**
	 * 获取权限字符串列表
	 */
	public List<String> getPermissions() {
		List<String> permissions = Lists.newArrayList();
		for (Menu menu : menus) {
			if (menu.getPermission()!=null && !"".equals(menu.getPermission())){
				permissions.add(menu.getPermission());
			}
		}
		return permissions;
	}

}
